/**
 * 
 */
package org.karthik.ecomm.util;

import java.util.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * 
 * This utility class is a singleton implementation for the SessionFactory
 * object. The getSession() method will return a Session object drawn from the
 * singleton SessionFactory object.
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory = null;

	public SessionFactory getSessionFactory() {
		try {
			if (sessionFactory == null) {
				/*Configuration cfg = new Configuration()
						.configure("ECommerce.cfg.xml");
				StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
				StandardServiceRegistry serviceRegistry = builder.build();
				sessionFactory = cfg.buildSessionFactory(serviceRegistry);*/
				
				Logger logger = Logger.getAnonymousLogger();
				logger.info("sessionFactory hashcode is"
						+ HibernateUtil.sessionFactory.hashCode());
			}
			return sessionFactory;
		} catch (Throwable ex) {
			ex.printStackTrace(System.out);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public Session getSession() throws HibernateException {
		if (HibernateUtil.sessionFactory != null) {
			Logger logger = Logger.getAnonymousLogger();
			logger.info("sessionFactory hashcode is"
					+ HibernateUtil.sessionFactory.hashCode());
		}
		return getSessionFactory().openSession();
	}

}
